import axios from 'axios'
//import QS from 'qs'
import Cookie from 'js-cookie'

const res = {
    code: 0,
    msg: "请求异常",
    data: {}
}
export async function request(url, data, type) {
    try {
        //http://localhost:8081
        url="http://localhost:8082"+url;
        //自动给请求加上token和collegeId，因为mock原因暂时注释掉
        try {
            // 获取登录数据
            if (Cookie.get('token') !== undefined) {
                const token = JSON.parse(Cookie.get('token'))
                // console.log('token信息',token)
                if (data !== undefined && data !== null ) {
                    data.token = token.token
                    // data.collegeId = token.collegeId
                } else {
                    data = {
                        token: token.token,
                        // collegeId: token.collegeId
                    }
                }
            }
        } catch (e) {
            //
        }
        // 判断请求类型
        if (type === 'get') {
            return (await axios.get(url, {params: data})).data
        } else if (type === 'post') {
            return (await axios.post(url, data,{headers: {'content-type': 'application/json;charset=utf-8'}})).data
        } else if(type === 'filePost'){
            console.log('发送filePost'+url)
            return (await  axios.post(url,data,{headers:{'Content-Type':'multipart/form-data'}})).data
        }else{
            return res
        }
    } catch (e) {
        return res
    }
}


